package org.example.bio;


import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

//阻塞io客户端
public class BioClient {

    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    sendHello();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }, "client-" + i).start();
        }
    }

    static void sendHello() throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1", 8081));
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(("hello world" + Thread.currentThread().getName()).getBytes());
        outputStream.flush();


        //阻塞操作 与输入输出等价  没读完就一直等待
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        outputStream.close(); // 关闭输出流
        socket.close();
    }
}
